/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller;

import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.actor.ClientModel;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.CodeNotExistsException;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.NullListException;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.ObjectNotFoundExeption;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.exception.TreaterNuloExeption;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.view.ClientView;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.observer.Observer;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.reference.ManagerReference;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.register.Register;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.controller.treater.ManagerNotification;
import java.io.Serializable;
import java.net.MalformedURLException;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.print.attribute.standard.Severity;
import br.ufrn.dimap.projetodetalhadodesoftware.chatrmi.model.util.Message;

/**
 *
 * @author jorge
 */
public class ClientControler extends  Register implements Serializable, ClientInterface, ClientControllerInterface {

    private ClientModel clientModel;
    public ClientView guiClient;
    private ManagerReference managerReference;
    private ManagerNotification managerNotification;
    private ChatInterface chatAtual;
    private ServerInterface serverInterface;


    public ClientControler(ClientModel clientModel, ClientView guiClient, String ip, int port) throws RemoteException, MalformedURLException  {
        super("rmi://"+ip+":"+port+"/", port);
        this.clientModel = clientModel;
        this.guiClient = guiClient;
        managerReference = ManagerReference.getInstance();
        managerNotification = ManagerNotification.getInstance();
    }
    
   

    public void update(Message message) throws RemoteException {
        try {
            managerNotification.treatNotification(this, message);
        } catch (CodeNotExistsException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (TreaterNuloExeption ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void sendMessage(String message) throws RemoteException {
        chatAtual.MessageFromClient(message);
    }

    public void getReferenceChat(ChatInterface chat) {
        try {
           chatAtual =  (ChatInterface) managerReference.getReference(ManagerReference.CHAT, chat.getURL());
        } catch (NotBoundException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ObjectNotFoundExeption ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void getReferenceServer() {
        try {
            serverInterface = (ServerInterface) managerReference.getReference(ManagerReference.SERVER, ServerControler.address);
        } catch (NotBoundException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (MalformedURLException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (RemoteException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        } catch (ObjectNotFoundExeption ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        }
  }

    public String getURL() throws RemoteException {
       return super.getUrl();
    }

    public void addChat(ChatInterface chat) throws RemoteException {
        clientModel.getListChats().add(chat);
    }

    public List<ChatInterface> getListChats() throws RemoteException{
        try {
            List listaChats = serverInterface.returnListToChatRooms();
            return listaChats ;
           
        } catch (NullListException ex) {
            Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
        }
        return null;
    }

    public void removeChat(Integer idChat) {
        for(int i = 0; i < clientModel.getListChats().size(); i++){
            try {
                if (clientModel.getListChats().get(i).getId() == idChat) {
                    clientModel.getListChats().remove(i);
                }
            } catch (RemoteException ex) {
                Logger.getLogger(ClientControler.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void createRegister(String name) throws RemoteException, MalformedURLException {
        clientModel.setName(name);
        super.setUrl(super.getUrl()+name);
        super.register();
    }

    public void connectChat() throws RemoteException {
        chatAtual.Attach(this);
    }

    public void disconnectChat() throws RemoteException {
        chatAtual.Detach(this);
    }

    public String getName() throws RemoteException {
        return clientModel.getName();
    }



}
